
package tankattack.sprites;

import gamepack.*;
import java.awt.event.KeyEvent;
import java.util.Vector;

import java.awt.*;

/** @author Somanath
	@version 1.1
*/

interface SpriteStates { public int RUNLEFT=10,RUNRIGHT=11,JUMPING=12; }
interface SpriteTypes  { public int MARIO=0,LUIGI=1; }
interface SpriteFrames { public int F_STAND_LEFT=0,F_RUN_LEFT=1,F_STAND_RIGHT=2,F_RUN_RIGHT=4,F_BEND_LEFT=5,F_BEND_RIGHT=6; }
interface SpriteJoypad { public int LEFT=0,RIGHT=1,UP=2,DOWN=3, ACTION=4, JUMP=5; }

public class Mario extends Sprite
implements SpriteStates, SpriteTypes, SpriteFrames, SpriteJoypad 
{

       public static Resource r = ResourceLoader.getResource("tankattack.sprites.Mario");

	   static {
	   //System.out.println("#########################  Mario.java Compiled ##########################");
	   }
	   int[] vk;
	   MotionController mc;
	   public Joypad j;

	//<Data name="spawnformat"> x=%d:0,y=%d:0,curFrame=%d:0:(0-LEFT,2-RIGHT) </Data>	
	public Mario() { // Mandatory - x,y,curFrame
		SpriteArgs sa = Sprite.getArgs();
		res = r;
		dir = curFrame;
		int[] options = null;
		
		setAttribute( "PLAYER", "BLOCK" );
		blockers = Environment.getAttributes("BLOCK");

		int[] defaultOption = { KeyEvent.VK_A, 	KeyEvent.VK_D, 		KeyEvent.VK_W, 	KeyEvent.VK_S,	  KeyEvent.VK_V,		KeyEvent.VK_B,	};

		if( options == null ) {
			vk = defaultOption;
		} else vk = options;

		mc = new TopViewMotionController(this,5);
		j = new Joypad(Application.key,vk);
		startSprite();
	
	}	
	public Mario(int x,int y,int dir,int[] options) {
		super(x,y, 0,0,r);
		    dir = curFrame; // Note: dir and curFrame are in sync for Tank class..
			setAttribute( "PLAYER", "BLOCK" );
			blockers = Environment.getAttributes("BLOCK");

		int[] defaultOption = { KeyEvent.VK_A, 	KeyEvent.VK_D, 		KeyEvent.VK_W, 	KeyEvent.VK_S,	  KeyEvent.VK_V,		KeyEvent.VK_B,	};

		if( options == null ) {
			vk = defaultOption;
		} else vk = options;

		mc = new SideViewMotionController(this,5);
		j = new Joypad(Application.key,vk);
		startSprite();
	}

	public void run() {

		//	switch( state) {
		//		case DYING:		Sprite.spawn("tankattack.sprites.Explosion # x="+x+",y="+y+",type=0",null); setState(DEAD); break;
	//			case NORMAL:	normal();	 		break;
			//		case 0:		runLeft();     break;
	//			case 1:      runRight();     break;
		//		case 2:     jumping();         break;
			//}
			normal();

	}


//// Usually other sprites may set state to EXPLODING/DYING rather than DEAD
	public void normal() {
           mc.move(j);
	}

	void runLeft() {
	}

	void runRight() {
	}

	void jumping() {
	}

}
/*
	public static Vector family = createFamily();
	public static String[][] str ={
		{ "mario_stand_left.gif", "mario_run_left.gif",  "mario_stand_right.gif", "mario_run_right.gif", "mario_bend_left.gif", "mario_bend_right.gif" },
		{ "mario_stand_left.gif", "mario_run_left.gif",  "mario_stand_right.gif", "mario_run_right.gif", "mario_bend_left.gif", "mario_bend_right.gif" }};

	public static CImage[][] skins = ResourceLoader.getCImage(str);

	static final boolean key[] = Application.key;
	int[] vk;	// Indirection (maps JoyPad to ActualKeyboard)
	int type;
	TimeStamp ts = new TimeStamp();
	//--------------------------------------------------------------------

	public Player(final int x,final int y,int type,int curFrame,int[] options) {
		super(x,y, skins[type],curFrame,10);
			setAttributes(PLAYER + BLOCK );
			blockers = new Vector[] { Sprite.blocks };

		this.type = type;
		int[] defaultOption1 = { KeyEvent.VK_LEFT, 	KeyEvent.VK_RIGHT, 	KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_CONTROL,	KeyEvent.VK_DELETE };
		int[] defaultOption2 = { KeyEvent.VK_A, 	KeyEvent.VK_D, 		KeyEvent.VK_W, 	KeyEvent.VK_S,	  KeyEvent.VK_V,		KeyEvent.VK_B,	};

		if( options == null ) {
			if(type == MARIO) vk = defaultOption1;
			if(type == LUIGI) vk = defaultOption2;
		} else vk = options;
		// // bottom justify frames 4,5 wrt frame 0
		startSprite();
	}


	public void run() {

		setDebugPainter( new Painter() { public void paint(Graphics g,int n) {
			g.drawString(" player1Life = " + player1Life + " keyPressTime.stamp " + keyPressTime.stamp,50,50);
		}});

		while( threadIsRunning() ) {
			switch( state) {
				case DYING:		new Explosion(x,y,Explosion.BIG); setState(DEAD);	break;
				case NORMAL:	normal();	 		break;
				case RUNLEFT:	runLeft();		break;
				case RUNRIGHT:	runRight();		break;
				case JUMPING:	jump();			break;
			}
		}
		stopSprite();
	}


	public void runLeft() {		// includes falling left
		int speed = 3;		curFrame = 0;	dir = Direction.WEST;
		do {
			if(ts.elapsed(10))	curFrame =(F_STAND_LEFT+F_RUN_LEFT) - curFrame;
			vx = -speed; vy = 0;
			move(vx,vy,0);
			if( !key[vk[LEFT]] ) { setState(NORMAL); return;}
			if( key[vk[JUMP]] && this.canChangeFrame(0) ) {	move(0,1,0); if(!canStep(SOUTH)) { setState(JUMPING); return; }	}
		} while( threadIsRunning() && state==RUNLEFT && tSleep() );
	}
	public void runRight() {
		int speed = 3;		curframe = 2;	dir = RIGHT_DIR;
		while( action == RUNRIGHT ) {
			if(ts.elapsed(10))	curframe = 5-curframe;
			xspeed = speed; yspeed = 4;
			moveTo(xspeed,yspeed,true);
			if( !key[vk[RIGHT]] ) { setAction(NORM); return; }
			if( key[vk[JUMPKEY]] && this.canChangeFrame(0) ) {	moveTo(0,1,false); if(!canMove(DOWN_DIR,true)) { setAction(JUMP); return; }	}
			if( !checkState() ) return;
			try { Thread.sleep(15); } catch(InterruptedException e) { sop( getClass().getName() + " Interruped: "+ e.getMessage()); }
		}
	}
	public void jump() {
		int count = 8;	int speed=2;
		while( action == JUMP ) {
			if(ts.elapsed(10))	{ count--;
				switch(dir) {
				case LEFT_DIR:	curframe = (curframe == 0||curframe ==1 )? 1-curframe : 0;	break;
				case RIGHT_DIR:	curframe = (curframe == 2||curframe ==3 )? 5-curframe : 2;	break;
				}
			}
			xspeed = 0; yspeed = -3;
			if( key[vk[RIGHT]] ){	curframe = 2;	dir=RIGHT_DIR; xspeed=speed; }
			else if( key[vk[LEFT]]  ) { curframe = 0;	dir=LEFT_DIR;  xspeed=-speed;  }
			moveTo(xspeed,yspeed,true);
			if( key[vk[SPECIAL]] && specialts.elapsed(250) ) { doSpecial();	}
			if( count <=0 ) { setAction(NORM); return;}
			if( !checkState() ) return;
			try { Thread.sleep(15); } catch(InterruptedException e) { sop( getClass().getName() + " Interruped: "+ e.getMessage()); }
		}
	}
	public void normal() {
		do {
			if( key[vk[RIGHT]] && canChangeFrame(2) ) { setState(RUNRIGHT); return; }
			if( key[vk[LEFT]] && canChangeFrame(0) ) { setState(RUNLEFT); return; }
			//if( key[vk[JUMPKEY]] && canChangeFrame(0) ) {	moveTo(0,1,false); if(!canMove(DOWN_DIR,true)) { setAction(JUMPING); return; }	}
			//if( key[vk[SPECIAL]] && specialts.elapsed(250) ) { doSpecial();	}
			curFrame = (dir==Direction.WEST)? 0 : 2;
			//if( key[vk[DOWN]] && this.canChangeFrame(4) ) { curframe = dir==LEFT_DIR ? 4 : 5;  }
			vx = 0; vy = 3;
			move(vx,vy,0);
		} while( threadIsRunning() && state==NORMAL && tSleep() );
	}

}
*/